﻿using System;
using System.Web.UI;

namespace KPI
{
	public partial class KPIEdit : BasePage
	{
		private DataAccess.Models.KPI _TheKPI;
		private DataAccess.Repository.IKPIRepository _KPIRepository;

		public int TheKPIID
		{
			get
			{
				return CommonHelper.QueryStringInt("kpiid");
			}
		}

		public DataAccess.Models.KPI TheKPI
		{
			get
			{
				if(_TheKPI == null && TheKPIID > 0)
				{
					if(_KPIRepository == null)
					{
						_KPIRepository = new DataAccess.Repository.KPIRepository();
					}

					_TheKPI = _KPIRepository.GetTheKPI(TheKPIID);
				}

				return _TheKPI;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!CanEditTheKPI())
			{
				Response.Redirect("~/KPIList.aspx");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindTheKPI();
			}
		}

		protected void cusvKPIContent_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 500);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void cusvActionPlan_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 500);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				if(TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Request_Review_Approved || TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Not_Approved)
				{
					if(AddKPIForUpdate())
					{
						Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
					}
					else
					{
						ShowMessage("出错了,请稍后再试或联系技术人员.", true);
					}
				}
				else
				{
					if(SaveTheKPI())
					{
						Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
					}
					else
					{
						ShowMessage("出错了,请稍后再试或联系技术人员.", true);
					}
				}
			}
		}

		protected void btnDelete_Click(object sender, EventArgs e)
		{
			if(DeleteKPI(false))
			{
				Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
			}
			else
			{
				ShowMessage("删除时出错了,请稍后再试或联系技术人员.", true);
			}
		}

		protected void btnDeleteComplete_Click(object sender, EventArgs e)
		{
			if(DeleteKPI(true))
			{
				Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
			}
			else
			{
				ShowMessage("删除时出错了,请稍后再试或联系技术人员.", true);
			}
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_KPIRepository != null)
			{
				((IDisposable)_KPIRepository).Dispose();
			}
		}

		private bool DeleteKPI(bool deleteComplete)
		{
			var result = false;

			if(TheKPIID > 0)
			{
				if(_KPIRepository == null)
				{
					_KPIRepository = new DataAccess.Repository.KPIRepository();
				}

				result = _KPIRepository.DeleteKPI(TheKPIID, deleteComplete);
			}

			return result;
		}

		private bool SaveTheKPI()
		{
			var result = false;

			if(TheKPI != null)
			{
				TheKPI.ActionPlans = txtActionPlan.Text.Trim();
				TheKPI.FinishTime = Convert.ToDateTime(txtKPIFinishTime.Text.Trim());
				TheKPI.GroupID = TheCurrentLoginUser.DeptID;
				TheKPI.KPIContent = txtKPIContent.Text.Trim();
				TheKPI.ResponsablePerson = txtKPIResponsablePerson.Text.Trim();
				TheKPI.ResponseDepartment = txtKPIResponseDept.Text.Trim();
				TheKPI.ResponsibleLeadership = txtKPIResponsibleLeadership.Text.Trim();
				TheKPI.RLTel = txtKPIRLTel.Text.Trim();
				TheKPI.RPTel = txtKPIRPTel.Text.Trim();
				TheKPI.StartTime = Convert.ToDateTime(txtKPIStartTime.Text.Trim());

				if(TheKPI.CurrentState == DataAccess.Models.KPIStates.Review_Not_Approved)
				{
					TheKPI.PreState = TheKPI.CurrentState;
					TheKPI.CurrentState = DataAccess.Models.KPIStates.Review_Pending;
				}

				if(_KPIRepository == null)
				{
					_KPIRepository = new DataAccess.Repository.KPIRepository();
				}

				result = _KPIRepository.UpdateKPI(TheKPI, false);
			}

			return result;
		}

		private bool AddKPIForUpdate()
		{
			var result = false;

			if(TheKPI != null)
			{
				var theKPIForUpdate = new DataAccess.Models.KPIForUpdate();

				theKPIForUpdate.KPIID = TheKPIID;
				theKPIForUpdate.UpdatedActionPlan = txtActionPlan.Text.Trim();
				theKPIForUpdate.UpdatedFinishTime = Convert.ToDateTime(txtKPIFinishTime.Text.Trim());
				theKPIForUpdate.UpdatedKPIContent = txtKPIContent.Text.Trim();
				theKPIForUpdate.UpdatedResponsablePerson = txtKPIResponsablePerson.Text.Trim();
				theKPIForUpdate.UpdatedResponseDepartment = txtKPIResponseDept.Text.Trim();
				theKPIForUpdate.UpdatedResponsibleLeadership = txtKPIResponsibleLeadership.Text.Trim();
				theKPIForUpdate.UpdatedRLTel = txtKPIRLTel.Text.Trim();
				theKPIForUpdate.UpdatedRPTel = txtKPIRPTel.Text.Trim();
				theKPIForUpdate.UpdatedStartTime = Convert.ToDateTime(txtKPIStartTime.Text.Trim());

				if(_KPIRepository == null)
				{
					_KPIRepository = new DataAccess.Repository.KPIRepository();
				}

				if(_KPIRepository.CreateKPIForUpdate(theKPIForUpdate))
				{
					TheKPI.PreState = TheKPI.CurrentState;
					TheKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Pending;

					result = _KPIRepository.UpdateKPI(TheKPI, false);
				}
			}

			return result;
		}

		private void BindTheKPI()
		{
			if(TheKPI != null)
			{
				txtActionPlan.Text = TheKPI.ActionPlans ?? string.Empty;
				txtKPIContent.Text = TheKPI.KPIContent ?? string.Empty;
				txtKPIFinishTime.Text = TheKPI.FinishTime.HasValue ? TheKPI.FinishTime.Value.ToString("yyyy-MM-dd") : string.Empty;
				txtKPIResponsablePerson.Text = TheKPI.ResponsablePerson ?? string.Empty;
				txtKPIResponseDept.Text = TheKPI.ResponseDepartment ?? string.Empty;
				txtKPIResponsibleLeadership.Text = TheKPI.ResponsibleLeadership ?? string.Empty;
				txtKPIRLTel.Text = TheKPI.RLTel ?? string.Empty;
				txtKPIRPTel.Text = TheKPI.RPTel ?? string.Empty;
				txtKPIStartTime.Text = TheKPI.StartTime.HasValue ? TheKPI.StartTime.Value.ToString("yyyy-MM-dd") : string.Empty;
			}
		}

		/// <summary>
		/// 检查是否有编辑该KPI的权限
		/// </summary>
		/// <returns></returns>
		private bool CanEditTheKPI()
		{
			var result = false;

			if(TheCurrentLoginUser != null)
			{
				if(!string.IsNullOrWhiteSpace(TheCurrentLoginUser.RoleName) && TheCurrentLoginUser.RoleName.Equals("unitadministrators"))
				{
					if(TheKPI != null && TheKPI.GroupID == TheCurrentLoginUser.DeptID)
					{
						result = true;
					}
				}
			}

			return result;
		}
	}
}